import 'package:flutter/material.dart';

class KeepState extends StatefulWidget {
  const KeepState({Key? key, required this.name}) : super(key: key);
  final String name;

  @override
  _KeepStateState createState() => _KeepStateState();
}

//混入AutomaticKeepAliveClientMixin保持状态，然后重写wantKeppAlive 的值为true。
class _KeepStateState extends State<KeepState> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('喜欢${widget.name}: $_count %'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if(_count < 100) {
              _count++;
            }
          });
        },
        child: const Icon(Icons.add),
        heroTag: widget.name,
      ),
    );
  }
}
